package com.hotel_management_backstage.lin.service;

import com.hotel_management_backstage.lin.dao.SysUserMapper;
import com.hotel_management_backstage.lin.dao.SysUserRoleMapper;
import com.hotel_management_backstage.lin.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class SysUserService {
    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public SysUser selectById(Integer id){
        return userMapper.selectById(id);
    }

    public SysUser selectByName(String name) {
        return userMapper.selectByName(name);
    }

    public void insertUser(SysUser sysUser, Integer sysStaffRole){
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        userMapper.insertUser(sysUser);
        int id = sysUser.getId();
        sysUserRoleMapper.insertUserRole(id,sysStaffRole);
        System.out.println(id);
    }


    public void updateUser(SysUser sysUser,Integer sysStaffShowRole){
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        userMapper.updateUser(sysUser);
        sysUserRoleMapper.updateUserRole(sysUser.getId(),sysStaffShowRole);
    }

    public void deleteUserById(int id){
        userMapper.deleteUserById(id);
    }
}
